 <template>
 	<view class="calendar-content" v-if="showCalendar">
 		<text class="example-info"></text>
 		<view class="example-body hideOnPc">
 			<button class="calendar-button" type="button" @click="open"></button>
 		</view>
 		<uni-calendar ref="calendar" class="uni-calendar--hook" :clear-date="true" :insert="info.insert"
 			:lunar="info.lunar" :startDate="info.startDate" :endDate="info.endDate" :range="info.range"
 			@confirm="confirm" @close="close" :showMonth="false" />
 	</view>
 </template>
 <script>
 	import {
 		mapMutations
 	} from 'vuex'
 	import {
 		eventBus
 	} from '@/eventBus.js';
 	/**
 	 * 获取任意时间
 	 */
 	function getDate(date, AddDayCount = 0) {
 		if (!date) {
 			date = new Date()
 		}
 		if (typeof date !== 'object') {
 			date = date.replace(/-/g, '/')
 		}
 		const dd = new Date(date)

 		dd.setDate(dd.getDate() + AddDayCount) // 获取AddDayCount天后的日期

 		const y = dd.getFullYear()
 		const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1 // 获取当前月份的日期，不足10补0
 		const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号，不足10补0
 		return {
 			fullDate: y + '-' + m + '-' + d,
 			year: y,
 			month: m,
 			date: d,
 			day: dd.getDay()
 		}
 	}
 	export default {
 		components: {},
 		data() {
 			return {
 				value: '',
 				dateValue: [],
 				showCalendar: false,
 				openCalender: false,
 				info: {
 					lunar: true,
 					range: true,
 					insert: false,
 					selected: []
 				}
 			}
 		},
 		mounted() {
 			// 监听事件并更新属性值
 			eventBus.$on('updateValue', value => {
 				this.value = value;
 				this.openCalender = this.value
 			});
 		},
 		beforeUpdate() {
 			this.open()
 		},
 		onReady() {
 			this.$nextTick(() => {
 				this.showCalendar = true
 			})
 		},
 		methods: {
 			...mapMutations('timeArray', ['updateTimeArray']),
 			open() {
 				if (this.openCalender === this.value) {
 					this.$refs.calendar.open()
 				}
 				this.openCalender = false
 				this.value = ''
 			},
 			close() {
 			},
 			confirm(e) {
 				// 获取之后的日期
 				let after = e.range.after
 				let afterArray = after.split("-");
 				let intAfterArray = afterArray.map(Number)


 				// 获取之前的日期
 				let before = e.range.before

 				// 获取当前日期
 				let nowDate = getDate(new Date(), 0).fullDate
 				let nowDateArray = nowDate.split("-");
 				let intNowDateArray = nowDateArray.map(Number)

 				// 将日期添加给dateValue传给订单页面
 				this.dateValue.push(before)
 				this.dateValue.push(after)

 				this.updateTimeArray(this.dateValue)

 				if (intAfterArray[0] >= intNowDateArray[0] && intAfterArray[1] === intNowDateArray[1] && intAfterArray[
 						2] > intNowDateArray[2] || intAfterArray[1] > intNowDateArray[1]) {
 					uni.navigateTo({
 						url: '/subpkg/Orders/Orders'
 					})
 				} else {
 					uni.showToast({
 						title: '请重新您的行程日期',
 						icon: 'success',
 						duration: 1500
 					})
 				}
 			}
 		},
 	}
 </script>
 <style lang="scss">
 	.example-body {
 		/* #ifndef APP-NVUE */
 		display: flex;
 		/* #endif */
 		flex-direction: row;
 	}

 	.calendar-button {
 		flex: 1;
 		font-weight: bold;
 		font-size: 32rpx;
 	}
 </style>